<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
/**
 * @author zheng
 * @date 2019/2/22
 * 微信支付
 */
namespace app\index\service;
use think\Cache;
use think\Db;
class LoginService {
    //商户信息
    protected  $appid;
    protected  $appkey;

    function __construct($appid,$appkey)
    {
        $this->appid  =  $appid;
        $this->appkey =  $appkey;
    }
    /**
     * @author zhegn
     * @date 2019/2/22
     * 获取3rd
     */
    public function get_key($code){

        $openid = $this -> get_openid($code);
        if(isset($openid['status'])){
            return $openid;
        }
        if(empty($openid['openid'])|| empty($openid['session_key'])){
           return (['message'=>'获取失败','status'=>'0']);
        }
        $wxopenid =$openid['openid'];
        $wxkey = $openid['session_key'];

        $session_key = $this -> rdsession();

        //Cache::set($session_key,$wxopenid.$wxkey,2400);
        //Cache::set('appkey',$wxkey,2400);
        //Cache::set('openid',$wxopenid,2400);//openid
        //Cache::set('rd_session',$session_key,2400);//openid
        $user_msg = array(
            'appkey'=>$wxkey,
            'openid'=>$wxopenid,
            'rd_session'=>$session_key,
            $session_key =>$wxopenid.$wxkey
        );
        Cache::set($session_key,$user_msg,2400);
        return $session_key;
    }

    /**
     * @author zheng
     * @date 2019/2/22
     * 获取openid等
     */
    public function get_openid($code){
        if(!$code){
            return (['message' =>'非法code','status'=>'0' ]);
        }

        $types = "authorization_code";
        $url ="https://api.weixin.qq.com/sns/jscode2session?appid={$this->appid}&secret={$this->appkey}&js_code={$code}&grant_type={$types}";
        $httpres = $this->httpcurl($url);
        if(!empty($httpres['openid'])){
            return $httpres;
        }else{
            return (['message'=>'获取失败','status'=>'0']);
        }
    }
    /**
     * @author zheng
     * @date 2019/2/22
     * curl
     */
    public function httpcurl($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, array());
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  //跳过ssl检查
        $output =  curl_exec($ch);
        $info = curl_getinfo($ch);
        if($output === false) {
            $output = "No cURL data returned for $url [". $info['http_code']. "]";
            if (curl_error($ch)){
                $output .= "\n". curl_error($ch);
            }
        }
        curl_close($ch);
        return json_decode($output,true);
    }

    /**
     * @author zheng
     * @date 2018/02/22
     * 生成3rd_session
     */
    public  function  rdsession(){
        $ression_rd = '';
        $session_code ="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
        $lenth = strlen($session_code)-1;
        for($i= 1;$i<=16;$i++){
            $ression_rd .= $session_code[rand(0,$lenth)];
        }
        return $ression_rd;
    }

    /**
     * linux生成3rd_session
     */
    function _3rd_session($len)
    {
        $fp = @fopen('/dev/urandom', 'rb');
        $result = '';
        if ($fp !== FALSE)
        {
            $result .= @fread($fp, $len);
            @fclose($fp);
        } else {
            trigger_error('Can not open /dev/urandom.');
        }
    // convert from binary to string
        $result = base64_encode($result);
    // remove none url chars
        $result = strtr($result, '+/', '-_');
        return substr($result, 0, $len);
}

}
